﻿using Helper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfControlDemo.Models;
using WPFCouse.Models;

namespace WpfControlDemo.数据模版.多层数据模板
{
    /// <summary>
    /// HierarchicalDataTemplateWindow.xaml 的交互逻辑
    /// </summary>
    public partial class HierarchicalDataTemplateWindow : Window
    {
        public HierarchicalDataTemplateWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //所有菜单数据
            List<MenuInfo> menuList = GetMenuList();
            List<MenuItemModel> listMenus = new List<MenuItemModel>();
            //将menuList转换为listMenus
            AddMenuItems(menuList, listMenus, null, 0);
            stuMenus.ItemsSource = listMenus;
            tvList.ItemsSource = listMenus;
        }

        /// <summary>
        /// 递归生成菜单项
        /// </summary>
        /// <param name="allMenus"></param>
        /// <param name="list"></param>
        /// <param name="pMenu"></param>
        /// <param name="parentId"></param>
        private void AddMenuItems(List<MenuInfo> allMenus, List<MenuItemModel> list, MenuItemModel pMenu, int parentId)
        {
            var subList = allMenus.Where(m => m.ParentId == parentId);//子菜单数据 
            foreach (MenuInfo mi in subList)
            {
                MenuItemModel miInfo = new MenuItemModel();
                miInfo.MenuId = mi.MenuId;
                miInfo.MenuName = mi.MenuName;
                miInfo.MKey = mi.MKey;
                if (pMenu != null)
                    pMenu.SubItems.Add(miInfo);
                else
                    list.Add(miInfo);
                //以当前创建的菜单项为父级，去添加它的子项集合
                AddMenuItems(allMenus, list, miInfo, mi.MenuId);//递归思想
            }

        }

        /// <summary>
        /// 获取菜单信息列表
        /// </summary>
        /// <returns></returns>
        public List<MenuInfo> GetMenuList()
        {
            List<string> strings = new List<string>() {"A","B", "C", "D", "E", "F" };
            List<MenuInfo> list = new List<MenuInfo>();
            for (int i = 1; i <= 6; i++)
            {
                MenuInfo menuInfo = new MenuInfo();
                menuInfo.MenuId = i;
                menuInfo.MenuName = $"菜单项{i}";
                menuInfo.ParentId = 0;
                menuInfo.MKey = "";
                list.Add(menuInfo);
                for (int j = 1; j <= 6; j++)
                {
                    MenuInfo menuInfo1 = new MenuInfo();
                    menuInfo1.MenuId = 6+j;
                    menuInfo1.MenuName = $"子菜单项{j}";
                    menuInfo1.ParentId = i;
                    menuInfo1.MKey = $"Ctrl+{strings[j - 1]}";
                    list.Add(menuInfo1);
                }
            }
            return list;
        }
    }
}
